<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>卢斌</title>
    <link rel="stylesheet" type="text/css" href="easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="easyUI/themes/icon.css">
    <script type="text/javascript" src="easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="easyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="javaScript/iphost.js"></script>
    <script type="text/javascript" src="javaScript/datagrid-filter.js"></script>
</head>
<body class="easyui-layout">
<script>
    $(function () {
        var machineid = 0;
        listurl = ipAddress + "getPartListOfMachine";
        loadmachineurl = ipAddress + "getAllMachine";
        var dg = $('#machinetable').datagrid({
            url:loadmachineurl,
            border:false,
            method:'post',
            queryParams:{
                account: localStorage.getItem("userAccount"),
                password: localStorage.getItem("password")
            },
            striped: true,
            singleSelect:true,
            idField: 'machineID',
            fit:true,
            fitColumns:true,
            multiSort:true,
            loadMsg:'^_^请稍等',
            emptyMsg: '您还没有添加任何产品信息',
            pagination:true,
            pageSize: 10000,
            remoteSort:false,
            sortOrder: 'asc',
            pageList: [10000, 20, 30],
            onClickRow:function(index,row){
                var clickrow = $('#machinetable').datagrid('getSelections');
                var queryParams = $('#pgmachine').datagrid('options').queryParams;
                queryParams.account = localStorage.getItem("userAccount");
                queryParams.password = localStorage.getItem("password");
                queryParams.machineID = clickrow[0].machineID;
                $('#pgmachine').datagrid('options').queryParams=queryParams;
                $('#pgmachine').datagrid('reload');
            },
            columns:[[
                {
                    field: 'machineID',
                    title: '编号',
                    width: '17%',
                    align: 'center',
                    sortable : true,
                    sorter: function (a, b) { return (a > b ? 1 : -1) }
                },
                {
                    field: 'machineName',
                    title: '产品名称',
                    width: '41%',
                    align: 'center',
                    sortable : true,
                    sorter: function (a, b) { return (a > b ? 1 : -1) }
                },
                {
                    field: 'machineValue',
                    title: '原料成本[元/千克]',
                    width: '22%',
                    align: 'center',
                    sortable : true,
                    formatter: function(value,row,index) {
                        return value.toFixed(3);
                    },
                    sorter: function (a, b) { return (a > b ? 1 : -1) }
                },
                {
                    field: 'machineDescription',
                    title: '备注',
                    width: '24%',
                    align: 'center',
                    formatter: function(value,row,index) {
                        if(value){
                            if(value.length == 0){
                                return '尚未添加备注';
                            }else{
                                return "<span title='" + value + "'>" + value + "</span>";
                            }
                        }else{
                            return '尚未添加备注';
                        }
                    }
                }
            ]]
        });
        dg.datagrid('enableFilter');
        var fbutton = dg.datagrid('getPager');
        fbutton.pagination({
            buttons:[
                {
                    iconCls: 'icon-remove',
                    handler: function() {
                        var clickrow = $('#machinetable').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg:'请先选择生产完成的产品',
                                timeout:1900,
                                showType:'slide'
                            });
                        }else {
                            $.messager.prompt('库存信息修改', '输入该产品生产数量:', function(r){
                                if(r){
                                    if(!/^\d+$/.test(r)){
                                        alert("请输入整数");
                                    }else {
                                        var producturl = ipAddress + "productMachine";
                                        $.ajax({
                                            type:'post',
                                            dataType:'json',
                                            url:producturl,
                                            data:{
                                                account:localStorage.getItem("userAccount"),
                                                password:localStorage.getItem("password"),
                                                machineID:clickrow[0].machineID,
                                                cutNumber:r
                                            },
                                            success:function (data) {
                                                if(data.state == 1){
                                                    var queryParams = $('#pgmachine').datagrid('options').queryParams;
                                                    queryParams.account = localStorage.getItem("userAccount");
                                                    queryParams.password = localStorage.getItem("password");
                                                    queryParams.machineID = clickrow[0].machineID;
                                                    $('#pgmachine').datagrid('options').queryParams=queryParams;
                                                    $('#pgmachine').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: data.message,
                                                        timeout:1900,
                                                        showType:'slide'
                                                    });
                                                }else {
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: data.message,
                                                        timeout:3000,
                                                        showType:'slide'
                                                    });
                                                }
                                            },
                                            error:function (e) {
                                                alert("未知错误");
                                            }
                                        });
                                    }
                                }else {
                                    $.messager.show({
                                        title:'提示',
                                        msg:'请输入产品完成生产的数量',
                                        timeout:1900,
                                        showType:'slide'
                                    });
                                }
                            });
                        }
                    }
                },
                {
                    iconCls: 'icon-edit',
                    handler: function() {
                        var clickrow = $('#machinetable').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg:'请选择要修改名称的产品',
                                timeout:2000,
                                showType:'slide'
                            });
                        }else {
                            $.messager.prompt('产品名称修改', '请输入该产品新名称:', function(r){
                                if(r){
                                    var renameurl = ipAddress + "machineReName";
                                    $.ajax({
                                        type:'post',
                                        dataType:'json',
                                        url:renameurl,
                                        data:{
                                            account:localStorage.getItem("userAccount"),
                                            password:localStorage.getItem("password"),
                                            machineID: clickrow[0].machineID,
                                            newName:r
                                        },
                                        success:function (data) {
                                            $('#machinetable').datagrid("reload");
                                            $.messager.show({
                                                title:'提示',
                                                msg: data.message,
                                                timeout:2000,
                                                showType:'slide'
                                            });
                                        },
                                        error:function (e) {
                                            alert("请求失败");
                                        }
                                    });
                                }
                            });
                        }
                    }
                },{
                    iconCls: 'icon-add',
                    handler: function() {
                        $.messager.prompt('添加新产品', '请输入产品名称:', function(r){
                            if(r){
                                var addNew = ipAddress + "createNewProduct";
                                $.ajax({
                                    type:'post',
                                    dataType:'json',
                                    url:addNew,
                                    data:{
                                        account:localStorage.getItem("userAccount"),
                                        password:localStorage.getItem("password"),
                                        paramName:r
                                    },
                                    success:function (data) {
                                        $('#machinetable').datagrid("reload");
                                        $.messager.show({
                                            title:'提示',
                                            msg: data.message,
                                            timeout:2000,
                                            showType:'slide'
                                        });
                                    },
                                    error:function (e) {
                                        alert("请求失败");
                                    }
                                });
                            }
                        });
                    }
                },
                {
                    iconCls: 'icon-more',
                    handler: function() {
                        var clickrow = $('#machinetable').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg:'请选择修改备注的产品',
                                timeout:2000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.prompt('产品名称修改', '旧备注：'+clickrow[0].machineDescription, function(r){
                                if(r){
                                    if(r.trim().length == 0){
                                        $.messager.show({
                                            title:'提示',
                                            msg:'请输入有效备注信息',
                                            timeout:2000,
                                            showType:'slide'
                                        });
                                    }else{
                                        if(r.trim().length>=225){
                                            $.messager.show({
                                                title:'提示',
                                                msg:'备注长度仅限225字',
                                                timeout:2000,
                                                showType:'slide'
                                            });
                                        }else{
                                            var resetinf = ipAddress + "descriptionReset";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url: resetinf,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    machineID: clickrow[0].machineID,
                                                    newStr: r
                                                },
                                                success:function (returndata) {
                                                    if(returndata.state == 1){
                                                        $('#machinetable').datagrid("reload");
                                                    }
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: returndata.message,
                                                        timeout:2000,
                                                        showType:'slide'
                                                    });
                                                },
                                                error:function (e) {
                                                    alert("发生错误");
                                                }
                                            })
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
            ]
        });

        var pmg = $('#pgmachine').datagrid({
            url:listurl,
            border:false,
            method:'post',
            queryParams:{
                account: localStorage.getItem("userAccount"),
                password: localStorage.getItem("password"),
                machineID: machineid
            },
            striped: true,
            singleSelect:true,
            idField: 'partID',
            fit:true,
            fitColumns:true,
            ctrlSelect:false,
            loadMsg:'^_^请稍等',
            emptyMsg: '请先选择产品类型',
            pagination:true,
            pageSize: 9000,
            pageList: [9000, 13, 26],
            columns:[[
                {
                    field: 'partID',
                    title: '原料编号',
                    width: '16%',
                    align: 'center'
                },
                {
                    field: 'partName',
                    title: '原料名称',
                    width: '22%',
                    align: 'center'
                },
                {
                    field: 'remaining',
                    title: '库存量',
                    width: '16%',
                    align: 'center',
                    formatter: function(value,row,index) {
                        if(row.remaining <= row.waring_num){
                            return '<span style="color:red;">('+row.remaining+')</span>';
                        }else{
                            return value;
                        }
                    }
                },
                {
                    field: 'waring_num',
                    title: '警戒数量',
                    width: '16%',
                    align: 'center'
                },
                {
                    field: 'requreNumber',
                    title: '产品包含数',
                    width: '16%',
                    align: 'center'
                },
                {
                    field: 'needRmb',
                    title: '所需成本[元]',
                    width: '16%',
                    align: 'center',
                    formatter: function(value,row,index) {
                        return (row.rmb * row.requreNumber).toFixed(3);
                    }
                }
            ]]
        });
        pmg.datagrid('enableFilter');
        var fbuttonss = pmg.datagrid('getPager');
        fbuttonss.pagination({
            buttons:[
                {
                    iconCls: 'icon-sum',
                    handler: function() {
                        var selectoneRow = $('#machinetable').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请选择要统计的产品类型',
                                timeout:2300,
                                showType:'slide'
                            });
                        }else{
                            var tss = $('#pgmachine').datagrid('getData');
                            var inflist = tss.rows;
                            var sumvalue = 0.0;
                            for(var ii = 0;ii<inflist.length;ii++){
                                var ttsp = inflist[ii];
                                sumvalue = sumvalue + (ttsp.rmb * ttsp.requreNumber);
                            }
                            $.messager.alert('产品原料成本', selectoneRow[0].machineName + "的以上原料成本："+Number(sumvalue).toFixed(3)+"元",'info');
                        }
                    }
                },
                {
                    iconCls: 'icon-add',
                    handler: function() {
                        var selectoneRow = $('#machinetable').datagrid('getSelections');
                        if(selectoneRow == null || selectoneRow.length == 0){
                            $.messager.show({
                                title:'提示',
                                msg: '请先选择增加原料信息的产品类型',
                                timeout:2300,
                                showType:'slide'
                            });
                        }else{
                            $('#addpartByName').dialog({
                                title: selectoneRow[0].machineName,
                                closed: true,
                                cache: false,
                                modal: true
                            });
                            $('#addpartByName').dialog("open");
                        }
                    }
                },
                {
                    iconCls: 'icon-no',
                    handler: function() {
                        var clickrow = $('#machinetable').datagrid('getSelections');
                        var clickpart = $('#pgmachine').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'删除选中产品原料信息',
                                msg: '请先选择产品类型',
                                timeout:2300,
                                showType:'slide'
                            });
                        }else{
                            if(clickpart == null || clickpart.length == 0){
                                $.messager.show({
                                    title:'删除选中产品原料信息',
                                    msg: '请选择要删除的原料信息',
                                    timeout:2300,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.confirm('确认对话框', '确认是否为失误操作，点击ok执行删除操作？', function(r){
                                    if (r){
                                        var deleteparturl = ipAddress + "deleteOnePartFromMachine";
                                        $.ajax({
                                            type:'post',
                                            dataType:'json',
                                            url: deleteparturl,
                                            data:{
                                                account:localStorage.getItem("userAccount"),
                                                password: localStorage.getItem("password"),
                                                machineID: clickrow[0].machineID,
                                                partID: clickpart[0].partID
                                            },
                                            success:function (returndata) {
                                                $('#pgmachine').datagrid("reload");
                                                $('#machinetable').datagrid("reload");
                                                $.messager.show({
                                                    title:'提示',
                                                    msg: re.message,
                                                    timeout:2300,
                                                    showType:'slide'
                                                });
                                            },
                                            error:function (e) {
                                                alert("网络错误");
                                            }
                                        })
                                    }
                                });
                            }
                        }
                    }
                },
                {
                    iconCls: 'icon-filter',
                    handler: function() {
                        var clickrow = $('#machinetable').datagrid('getSelections');
                        var clickpart = $('#pgmachine').datagrid('getSelections');
                        if(clickrow == null || clickrow.length == 0){
                            $.messager.show({
                                title:'修改选中产品原料需量',
                                msg: '请先选择产品类型',
                                timeout:2300,
                                showType:'slide'
                            });
                        }else{
                            if(clickpart == null || clickpart.length == 0){
                                $.messager.show({
                                    title:'修改原料需量',
                                    msg: '请选择要修改的原料信息',
                                    timeout:2300,
                                    showType:'slide'
                                });
                            }else{
                                $.messager.prompt('原料需量信息修改', '请输入该原料在产品中的数量:', function(r) {
                                    if (r) {
                                        if (!/^\d+$/.test(r)) {
                                            alert("请输入正整数");
                                        }else{
                                            var cuturl = ipAddress+"changePartNeedNumber";
                                            $.ajax({
                                                type:'post',
                                                dataType:'json',
                                                url: cuturl,
                                                data:{
                                                    account:localStorage.getItem("userAccount"),
                                                    password:localStorage.getItem("password"),
                                                    machineID: clickrow[0].machineID,
                                                    partID: clickpart[0].partID,
                                                    needNum:r
                                                },
                                                success:function (datas) {
                                                    $('#pgmachine').datagrid('reload');
                                                    $('#machinetable').datagrid('reload');
                                                    $.messager.show({
                                                        title:'提示',
                                                        msg: datas.message,
                                                        timeout:2000,
                                                        showType:'slide'
                                                    });
                                                },
                                                error:function (e) {
                                                    alert("请求错误");
                                                }
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            ]
        });
    });
</script>
<div data-options="region:'east',split:false" title="所需原料" style="width:61%;">
    <table id="pgmachine" style="width:100%" class="easyui-datagrid"></table>
</div>
<div data-options="region:'center'">
    <table id="machinetable" class="easyui-datagrid"></table>
</div>

<div id="addpartByName" class="easyui-dialog" title="原料信息" data-options="iconCls:'icon-save',closed:true" style="width:400px;height:420px;padding:10px;text-align: center">
    <form id="mp">
        <div style="margin-bottom:20px">
            <input id="addinputpartName" class="easyui-textbox" name="schid" prompt="20字以内:" style="width:100%" data-options="label:'新原料名称:',required:true">
        </div>
        <div style="margin-bottom:20px">
            <input id="addinputNumber" class="easyui-numberbox" name="schid" prompt="0-999999999:" style="width:100%" data-options="label:'产品包含数:',required:true, min: 0, max: 999999999, precision:0">
        </div>
        <a id="submitpartmachine" href="javascript:void(0)" class="easyui-linkbutton" onclick="addpartmachine()" style="width:30%">确认</a>
    </form>
</div>
<script>
    function addpartmachine() {
        $('#submitpartmachine').linkbutton('disable');
        var name = document.getElementById("addinputpartName").value;
        var numberv = document.getElementById("addinputNumber").value;
        if(name == null || name.trim().length == 0){
            $.messager.show({
                title:'提示',
                msg: '请输入原料名',
                timeout:2300,
                showType:'slide'
            });
        }else{
            if(numberv == null || numberv <= 0){
                $.messager.show({
                    title:'提示',
                    msg: '请输入有效原料需求数量',
                    timeout:2300,
                    showType:'slide'
                });
            }else{
                var addpartmachineurl = ipAddress + "addOnePartToMachineByPartName";
                var selectoneRow = $('#machinetable').datagrid('getSelections');
                $.ajax({
                    type:'post',
                    dataType:'json',
                    url:addpartmachineurl,
                    data:{
                        account:localStorage.getItem("userAccount"),
                        password:localStorage.getItem("password"),
                        machineID: selectoneRow[0].machineID,
                        partName: name,
                        needNumber: numberv
                    },
                    success:function (re) {
                        $('#addpartByName').dialog("close");
                        $('#machinetable').datagrid("reload");
                        $('#pgmachine').datagrid("reload");
                        $('#submitpartmachine').linkbutton('enable');
                        $.messager.show({
                            title:'提示',
                            msg: re.message,
                            timeout:2300,
                            showType:'slide'
                        });
                    },
                    error:function (e) {
                        $('#addpartByName').dialog("close");
                        alert("请检查网络状况");
                        $('#submitpartmachine').linkbutton('enable');
                    }
                })
            }
        }
    }
</script>
</body>
</html>